﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiaryPlanner
{
    public partial class PlannerDay : UserControl
    {
        Planner planner;

        List<Label> dayList = new List<Label>();
        List<Label> timeList = new List<Label>();

        const int week = 7;
        const int time = 24;
        const int half = 2;
        const int labelNum = time * half;

        public PlannerDay()
        {
            InitializeComponent();
            initDay();
        }

        private void initDay()
        {
            int i;

            for (i = 0; i < labelNum; i++)
                dayList.Add(new System.Windows.Forms.Label());

            for (i = 0; i < labelNum; i++)
                timeList.Add(new System.Windows.Forms.Label());

            this.SuspendLayout();

            for (i = 0; i < labelNum; i++)
            {
                dayList[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                dayList[i].Location = new System.Drawing.Point(0, 0);
                dayList[i].Margin = new System.Windows.Forms.Padding(0);
                dayList[i].Name = "labelD" + i.ToString();
                dayList[i].Size = new System.Drawing.Size(54*week, 25);
                dayList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                timeList[i].Location = new System.Drawing.Point(0, 0);
                timeList[i].Margin = new System.Windows.Forms.Padding(0);
                timeList[i].Name = "labelT" + i.ToString();
                timeList[i].Size = new System.Drawing.Size(53, 25);
                if (i % 2 == 0) timeList[i].Text = getTime_KR(i / 2);
                else timeList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                this.flowLayoutPanel1.Controls.Add(timeList[i]);
                this.flowLayoutPanel1.Controls.Add(dayList[i]);
            }

            this.ResumeLayout(false);
            this.PerformLayout();

            //클릭 이벤트 핸들러 등록
            for (i = 0; i < dayList.Count; i++)
                dayList[i].Click += new System.EventHandler(this.dayList_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (i = 0; i < dayList.Count; i++)
                dayList[i].DoubleClick += new System.EventHandler(this.dayList_DoubleClick);
        }

        private string getTime_KR(int time)
        {
            string temp = "";

            if (time == 0)
                temp = "오전12시";
            else if (time < 12)
            {
                temp = "오전" + (time).ToString() + "시";
            }
            else if (time == 12)
                temp = "오후12시";
            else
            {
                temp = "오후" + (time - 12).ToString() + "시";
            }

            return temp;
        }

        private string getTime_colon(int time)
        {
            string temp;

            if (time == 0) temp = "00";
            else temp = (time / 2).ToString();

            if (time % 2 == 0) temp = temp + ":" + ((time % 2) * 30).ToString()+"0";
            else temp = temp + ":" + ((time % 2)*30).ToString();

            return temp;
        }

        private void dayList_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 0; i < dayList.Count; i++)
                dayList[i].BackColor = System.Drawing.Color.White;

            label.BackColor = System.Drawing.Color.Blue;
        }

        private void dayList_DoubleClick(object sender, EventArgs e)
        {
            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner, this);
            PlannerEditObject.ShowDialog();
        }

        public string dayTime(int time)
        {
            for (int i = 0; i < dayList.Count; i++)
            {
                if (dayList[i].BackColor == System.Drawing.Color.Blue)
                {
                    if (time == 0) return getTime_colon(i);
                    else return getTime_colon(i + 1);
                }
            }
            return null;
        }

        public void savePlanner(Planner planner)
        {
            this.planner = planner;
        }
    }
}
